package cn.wolfcode.crm.service.impl;



import cn.wolfcode.crm.domain.Menu;
import cn.wolfcode.crm.mapper.MenuMapper;
import cn.wolfcode.crm.service.IMenuService;
import cn.wolfcode.crm.util.MenuUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> selectAllParent() {

        return menuMapper.selectAllParent();
    }

    @Override
    public List<Integer> listIdsByRoleId(Long roleId) {

        return menuMapper.listIdsByRoleId(roleId);
    }

    @Override
    public List<Menu> listMenus() {
        //直接从session中去获取
        List<Menu> list = (List<Menu>) SecurityUtils.getSubject().getSession().getAttribute(MenuUtil.MENUS_IN_SESSION);
        return list;
    }


    @Override
    public List<Long> listMenuIdsByEmployeeId(Long employeeId) {
        List<Long> ids = menuMapper.listMenuIdsByEmployeeId(employeeId);
        return ids;
    }


    @Override
    public List<Menu> selectChildByParentId(Long parentId) {

        List<Menu>  list =  menuMapper.selectChildByParentId(parentId);
        return list;
    }

    @Override
    public void insert(Menu menu) {
        if (menu.getParent().getId() == null){
            menu.setUrl(null);
            menu.getParent().setId(null);
        }

        menuMapper.insert(menu);
    }

}
